/************************************************************************/
/*																		*/
/*		School Hangover Project											*/
/*																		*/
/*		IMAC 2010														*/
/*		Author : 	Helena Duchaussoy									*/
/*					Isabelle Gouwy										*/
/*					Nathalie Kuoch										*/
/*					Franck Letellier									*/
/*					Louise Pequignot									*/
/*																		*/
/*		E-mail: flo-in-underware@googlegroups.com						*/
/*																		*/
/************************************************************************/

#include "Room.h"

const char Room::className[] = "Room";

#define method(class, name) {#name, &class::name}

Lunar<Room>::RegType Room::methods[] = {
	method(Room, addFlowerPot),
	method(Room, addTable),
	method(Room, addEnemy),
	method(Room, addEnemy),
	method(Room, addFriend),
	method(Room, addCloth),
	method(Room, addClue),
};

Room::Room(const std::string& sname, const std::string& fname) : m_sfilename(sname) {
	m_pwall = new Wall(fname);
}

Room::Room(lua_State* L) {
}

Room::~Room() {
}

void Room::init() {
	// Lua initialisation
	m_pscript = lua_open();
	// Load Lua standard libraries
	luaL_openlibs(m_pscript);

	// Register class for Lua
	Lunar<Room>::Register(m_pscript);
	Lunar<FlowerPot>::Register(m_pscript);
	Lunar<Table>::Register(m_pscript);
	Lunar<Character>::Register(m_pscript);

	Lunar<Friend>::Register(m_pscript);
	Lunar<Cloth>::Register(m_pscript);
	Lunar<Clue>::Register(m_pscript);
	Lunar<Enemy>::Register(m_pscript);

	// Launch Lua script
	if (luaL_dofile(m_pscript,m_sfilename.c_str())!=0) {
		std::cerr << lua_tostring(m_pscript,-1) << std::endl;
		exit(0);
	}

	// Call Lua method
	lua_settop(m_pscript,0);
	Lunar<Room>::push(m_pscript,this);
	Lunar<Room>::call(m_pscript,"load",0,0);

	m_pwall->init();
}

void Room::update() {
	for(size_t i=0; i< m_viobject.size(); i++) {
		m_viobject[i]->update();
	}
	(*m_pwall).update();
	
}

int Room::addFlowerPot(lua_State* L) {
	FlowerPot* newObject = Lunar<FlowerPot>::check(L,1);
	if (newObject == NULL) {
		std::cerr << "Room add enemy : Wrong parameters" << std::endl;
		return 0;
	}

	m_viobject.push_back(newObject);

	return 0;
}

int Room::addTable(lua_State* L) {
	Table* newObject = Lunar<Table>::check(L,1);
	if (newObject == NULL) {
		std::cerr << "Room add table : Wrong parameters" << std::endl;
		return 0;
	}

	m_vobject.push_back(newObject);

	return 0;
}

int Room::addEnemy(lua_State* L) {
	Enemy* newObject = Lunar<Enemy>::check(L,1);
	if (newObject == NULL) {
		std::cerr << "Room add enemy : Wrong parameters" << std::endl;
		return 0;
	}

	m_viobject.push_back(newObject);

	return 0;
}

int Room::addFriend(lua_State* L) {
	Friend* newObject = Lunar<Friend>::check(L,1);
	if (newObject == NULL) {
		std::cerr << "Room add friend : Wrong parameters" << std::endl;
		return 0;
	}

	m_viobject.push_back(newObject);

	return 0;
}

int Room::addCloth(lua_State* L) {
	Cloth* newObject = Lunar<Cloth>::check(L,1);
	if (newObject == NULL) {
		std::cerr << "Room add cloth : Wrong parameters" << std::endl;
		return 0;
	}

	m_viobject.push_back(newObject);

	return 0;
}

int Room::addClue(lua_State* L) {
	Clue* newObject = Lunar<Clue>::check(L,1);
	if (newObject == NULL) {
		std::cerr << "Room add clue : Wrong parameters" << std::endl;
		return 0;
	}

	m_viobject.push_back(newObject);

	return 0;
}

bool Room::computeInteraction(Player* player){
	osg::Vec3d playerPos = player->getPosition();
	std::vector<InteractiveObject*>::iterator iter = m_viobject.begin();
	while (iter != m_viobject.end()){
		if((*iter)!= NULL){ 
			if((*iter)->getTransform()!=NULL){
				osg::Vec3d pos = (*iter)->getTransform()->getPosition();
				float dist = (pos-playerPos).length();
				if(dist < 3){
					osg::Vec3d position(pos.x(),pos.y(),pos.z()+3.);
					player->setPositionExclam(position);
					return true;
				}
				else{
					osg::Vec3d position(-100,-100,2);
					player->setPositionExclam(position);
				}
			}
		}
		++iter;
	}
	return false;
}
